import { Injectable } from '@nestjs/common';
import { CreateWeaponUpDto } from './dto/create-weapon-up.dto';
import { UpdateWeaponUpDto } from './dto/update-weapon-up.dto';
import { PageQuery } from '../../../util/entity/page.entity';
import { Role } from '../../admin/role/entities/role.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { WeaponUp } from './entities/weapon-up.entity';
import { Repository } from 'typeorm';
import { buildQuery } from '../../../util/common-fn';

@Injectable()
export class WeaponUpService {
  constructor(
    @InjectRepository(WeaponUp)
    private weaponUpRepository: Repository<WeaponUp>,
  ) {}
  create(createWeaponUpDto: CreateWeaponUpDto) {
    return this.weaponUpRepository.save(createWeaponUpDto);
  }

  findAll(pageQuery: PageQuery<Role>) {
    return this.weaponUpRepository.findAndCount(buildQuery(pageQuery));
  }

  findOne(id: number) {
    return this.weaponUpRepository.findOneByOrFail({ id });
  }

  update(id: number, updateWeaponUpDto: UpdateWeaponUpDto) {
    return this.weaponUpRepository.update({ id }, updateWeaponUpDto);
  }

  remove(id: number) {
    return this.weaponUpRepository.softDelete({ id });
  }

  getNext(nextWeaponUp: { weaponType: number; level: number }) {
    const { weaponType, level } = nextWeaponUp;
    return this.weaponUpRepository.findOneBy({
      weaponType,
      level,
    });
  }
}
